Ext.ns('Tm.task');
Tm.task.TaskCategoryPanel = Ext.extend(Ext.Panel, {
			id : 'taskCategoryPanel',
			frame : true,
			autoWidth : true,
			// autoHeight : true,
			// layout : 'fit',
			task : null,
			initComponent : function() {

				var form = new Tm.common.FormPanel({
							ref : 'form',

							defaults : {
								xtype : 'textfield',
								margins : '0 5 0 0'
							},
							items : [{
										fieldLabel : '分类',
										name : 'categoryId',
										hiddenName : 'categoryId',
										allowBlank : false,
										width : 500,
										valueField : 'key',
										displayField : 'value',
										typeAhead : true,
										xtype : 'commoncombobox',
										mode : 'remote',
										forceSelection : true,
										triggerAction : 'all',
										emptyText : '请选择...',
										selectOnFocus : true,

										store : new Ext.data.JsonStore({// /列表框中的选项数据
											url : 'category/getL2Category.do',
											fields : [{
														name : 'key'
													}, {
														name : 'value'
													}]
										}

										),
										listeners : {
											change : function(field, newValue, oldValue) {
												console.log(oldValue);
												if (oldValue == '') {
													field.setValue(newValue);
													firstGridStore.load();
												} else if (newValue != oldValue) {
													Ext.MessageBox.confirm('请确认', '您是否确认更改商标分类？（此操作将会清空全部已选子类）', function(btn) {
																if (btn == 'yes') {
																	field.setValue(newValue);
																	firstGridStore.load();
																	secondGridStore.removeAll();
																} else {
																	field.setValue(oldValue);
																}
															});

												}
											}

										}
									}

							]
						});

				var fields = [{
							name : 'id',
							mapping : 'id'
						}, {
							name : 'code',
							mapping : 'code'
						}, {
							name : 'cnName',
							mapping : 'cnName'
						}, {
							name : 'enName',
							mapping : 'enName'
						}];
				// create the data store
				var firstGridStore = new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
										url : 'category/queryCategory.do'
									}),
							reader : new Ext.data.JsonReader({
										totalProperty : 'totalProperty',
										root : 'root',
										id : 'id',
										fields : fields
									}),
							baseParams : {
								start : 0,
								limit : 20
							},
							sortInfo : {
								field : 'code',
								direction : 'ASC'
							},
							initLoad:false,
							// autoLoad : true,
							listeners : {
								beforeload : function() {
									//var loadCategoryId = Ext.getCmp('taskCategoryPanel').task.bsCategory.id;
									var categoryId = form.getForm().findField('categoryId').getValue();
									var parent = categoryId ? categoryId : Ext.getCmp('taskCategoryPanel').task.bsCategory?Ext.getCmp('taskCategoryPanel').task.bsCategory.id:null;
									if(!parent){
										
										return false;
									}
									Ext.apply(this.baseParams, {
												parent : parent
											});
								}
							}
						});

				var cm = new Ext.grid.ColumnModel([{
					id : 'id',
					header : 'id',
					width : 20,
					dataIndex : 'id'
						// ,
						// hidden : true
					}, {
					header : '编号',
					width : 80,
					dataIndex : 'code',
					sortable : true
				}, {
					header : '中文名称',
					width : 80,
					dataIndex : 'cnName',
					sortable : true

				}, {
					header : '英文名称',
					width : 80,
					dataIndex : 'enName',
					sortable : true,
					editor : new Ext.form.TextField({
								allowBlank : true,

								listeners : {

									change : function(field, newValue, oldValue) {
										Ext.Ajax.request({
													url : 'category/updateCategoryEnName.do?id=' + field.gridEditor.record.id,
													method : 'POST',
													params : {
														enName : newValue
													},
													success : function() {
														record.store.commitChanges();

													}
												});
									}
								}
							})
				}]);
				var cm2 = new Ext.grid.ColumnModel([{
					id : 'id',
					header : 'id',
					width : 20,
					dataIndex : 'id'
						// ,
						// hidden : true
					}, {
					header : '编号',
					width : 80,
					dataIndex : 'code',
					sortable : true
				}, {
					header : '中文名称',
					width : 80,
					dataIndex : 'cnName',
					sortable : true
				}, {
					header : '英文名称',
					width : 80,
					dataIndex : 'enName',
					sortable : true
				}]);

				// declare the source Grid
				var firstGrid = new Ext.grid.EditorGridPanel({
							id : 'firstGrid',
							ddGroup : 'secondGridDDGroup',
							store : firstGridStore,
							cm : cm,
							sm : new Ext.grid.RowSelectionModel(),
							enableDragDrop : true,
							stripeRows : true,
							title : '未选择子类'

						});

				var secondGridStore = new Ext.data.JsonStore({
							fields : fields,
							root : 'root'
						});

				// create the destination Grid
				var secondGrid = new Ext.grid.GridPanel({
							id : 'secondGrid',

							ddGroup : 'firstGridDDGroup',
							store : secondGridStore,
							cm : cm2,
							enableDragDrop : true,
							stripeRows : true,
							// autoExpandColumn : 'name',
							title : '已选择子类'
						});

				// used to add records to the destination stores
				var blankRecord = Ext.data.Record.create(fields);

				var panel2 = new Ext.Panel({

							frame : true,
							height : 400,
							autoWidth : true,

							layout : 'hbox',
							defaults : {
								flex : 1
							}, // auto stretch
							layoutConfig : {
								align : 'stretch'
							},
							items : [firstGrid, secondGrid]
						});

				var toolBar = new Ext.Toolbar({
							hideBorders : true,
							buttons : [{
										xtype : 'button',
										text : '保存',
										iconCls : 'act_addIcon',
										handler : function() {
											var store = Ext.getCmp('secondGrid').store;
											console.dir(store.data.keys);
											var keys = store.data.keys;
											if (form.getForm().isValid()) {
												Ext.Ajax.request({
															url : 'task/addCategoryListToTask.do',
															method : 'POST',
															params : {
																ids : keys,
																taskId : Ext.getCmp("taskCategoryPanel").task.id,
																categoryId : form.getForm().findField('categoryId').getValue()
															},
															success : function() {
																App.setAlert('ok', '操作成功');
															}
														});
											}

										}
									}]
						});

				Ext.apply(this, {
							tbar : toolBar,
							items : [form, panel2]
						});

				Tm.task.TaskCategoryPanel.superclass.initComponent.call(this);
			},
			listeners : {
				afterrender : function(comp) {
					/*******************************************************************************************************************************************
					 * Setup Drop Targets
					 ******************************************************************************************************************************************/
					// This will make sure we only drop to the view scroller element
					var firstGridDropTargetEl = Ext.getCmp('firstGrid').getView().scroller.dom;
					var firstGridDropTarget = new Ext.dd.DropTarget(firstGridDropTargetEl, {
								ddGroup : 'firstGridDDGroup',
								notifyDrop : function(ddSource, e, data) {
									var records = ddSource.dragData.selections;
									Ext.each(records, ddSource.grid.store.remove, ddSource.grid.store);
									Ext.getCmp('firstGrid').store.add(records);
									Ext.getCmp('firstGrid').store.sort('name', 'ASC');
									return true;
								}
							});

					// This will make sure we only drop to the view scroller element
					var secondGridDropTargetEl = Ext.getCmp('secondGrid').getView().scroller.dom;
					var secondGridDropTarget = new Ext.dd.DropTarget(secondGridDropTargetEl, {
								ddGroup : 'secondGridDDGroup',
								notifyDrop : function(ddSource, e, data) {
									var records = ddSource.dragData.selections;
									Ext.each(records, ddSource.grid.store.remove, ddSource.grid.store);
									Ext.getCmp('secondGrid').store.add(records);
									Ext.getCmp('secondGrid').store.sort('name', 'ASC');
									return true;
								}
							});
					this.loadData();

				}

			},
			loadData : function() {
				var task = this.task;
				var category = task.bsCategory;
				if (category) {
					this.form.getForm().findField('categoryId').setValue(category.id);
					// this.form.getForm().findField('categoryId').dataId = category.id;
					Ext.getCmp('firstGrid').store.on('load', function(firstStore, firstStoreRecords) {
								if(Ext.getCmp('firstGrid').store.initLoad){
									return;
								}
								var categoryStore = new Ext.data.JsonStore({
											proxy : new Ext.data.HttpProxy({
														url : 'task/getL3CategoriesByTaskId.do'
													}),
											fields : [{
														name : 'id',
														mapping : 'id'
													}, {
														name : 'code',
														mapping : 'code'
													}, {
														name : 'cnName',
														mapping : 'cnName'
													}, {
														name : 'enName',
														mapping : 'enName'
													}],
											baseParams : {
												taskId : task.id
											},
											sortInfo : {
												field : 'code',
												direction : 'ASC'
											},
											// autoLoad : true,
											listeners : {

												load : function(store, records) {

													for (var i = 0; i < records.length; i++) {
														if (firstStore.data.keys.indexOf(records[i].id) != -1) {
															firstStore.removeAt(firstStore.data.keys.indexOf(records[i].id));
															continue;
														}
													}
													Ext.getCmp('secondGrid').store.add(records);
													
													Ext.getCmp('firstGrid').store.initLoad=true;
												}
											}
										});
								categoryStore.load();
							});
					Ext.getCmp('firstGrid').store.load();

				}
			}
		});
